package org.brisling.medical.queryService;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.exception.DangerousOperatorException;
import org.brisling.common.util.DateUtil;
import org.brisling.medical.domain.OutpatientDiagnosis;
import org.brisling.sysman.queryService.QueryDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * <p>Title: QueryOutpatientDiagnosisService</p>
 * <p>Description: 门诊诊断查询服务类</p>
 * <p>Company: tongking</p>
 * @author jackson wang
 * @since 1.0
 * @version 1.0
 * @date 2017年2月21日 上午11:38:58
 */
@RestController
@RequestMapping("/medicalOutpatientDiagnosis/query")
public class QueryOutpatientDiagnosisService extends AbstractBaseQueryService<OutpatientDiagnosis>{
	
	
	@Autowired
	private QueryDictionaryService dicService;
	
	public QueryOutpatientDiagnosisService(){
		this.moduleName = ConstSysParam.MODULE_OUTPATIENT_DIAGNOSIS;
		this.subSystem = ConstSysParam.SUBSYSTEM_MEDICAL;
		this.tableName = ConstSysParam.VIEW_PREFIX +this.moduleName;
		this.clazz = OutpatientDiagnosis.class;
		
		typeMap = this.getTypeMap(clazz);
		
		orderMap.put("sortedid",null);
		
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)	
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return this.getBizMainPage();
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return this.getBizEditPage();
	}
	
	@RequestMapping("/getListDomain.action")
	public ListDomain<OutpatientDiagnosis> getListDomain(@RequestBody(required=false) String body){		
		
		return super.getListDomainByConditionAndOrder(body, typeMap, orderMap,false);
	}
	
	@RequestMapping("/getList.action")
	public List<OutpatientDiagnosis> getList(@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="deptid",required=false) Integer deptid,
			@RequestParam(value="sort",required=false) String sort){		
		
		StringBuilder sb = new StringBuilder();
		String sql = "";
		if(deptid!=null){
			String whereCorse=String.format(sb.append("\"deptid\" = %1$d").toString(),deptid);
			sql = String.format(this.TW_SQL, this.tableName,whereCorse);
		}else{
			sql = String.format(this.T_SQL, this.tableName);
		}
		
		return super.getPagedList(page,rows,sql);
	}
	
	/**
	 * 根据处方id查找处方信息
	 * @param id	处方id
	 * @return 处方信息记录 
	 */
	@RequestMapping("/getOne.action")
	public OutpatientDiagnosis getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOne(id);
	}
	
	/**
	 * 根据处方唯一编码查找处方诊断信息
	 * @param identify	处方唯一编码
	 * @return 处方诊断信息记录
	 */
	@RequestMapping("/getOneByIdentify.action")
	public OutpatientDiagnosis getOneByIdentify(@RequestParam(value="identify",required=false) String identify){	
		return super.getOneByIdentify(identify);
	}
	
	@RequestMapping("/getOneByField.action")
	public OutpatientDiagnosis getOneByField(@RequestParam(value="field",required=false) String field,@RequestParam(value="fieldVal",required=false) String fieldVal){	
		return super.getOneByIdentify(field,fieldVal);
	}
	
	//Outpatient
	/**
	 * 门诊急诊信息异常数据
	 * @return
	 */	

	public List<OutpatientDiagnosis> getOutpatientAbnormal(){		
		
		String sql = dicService.getSqlByBizcode("medical_outpatient");			
		//sql = new String("select * from medical_anti_dose");
		sql = String.format(sql, DateUtil.getYestodayMonthDay()[2]);
		try {
			return super.getClassedAllList(sql, OutpatientDiagnosis.class);
		} catch (DangerousOperatorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				 	
		return null;
	}
	
	
	public ListDomain<OutpatientDiagnosis> getSqlListDomain(String sql) {		
		return super.getSqlListDomain(sql);
	}
	
	/**
	 * 按照患者编码查询患者门诊记录，
	 * @param patient_no	患者编码
	 * @return
	 */
	public List<OutpatientDiagnosis> getListByPatientNo(String patient_no){
		
		if(patient_no!=null){
			String sql = "select * from %1$s where patient_no = %2$s order by feeat desc";
			sql = String.format(sql, this.tableName,patient_no);
		return super.getAllList(sql);
		}else{
			return null;
		}
	}
	
	/**
	 * 按照就诊序号查询患者诊断记录
	 * @param jzxh	患者编码
	 * @return
	 */
	@RequestMapping("/getDiagnosisList.action")
	public List<OutpatientDiagnosis> getListByJzxh(String jzxh){
		
		
		if(jzxh!=null){
			String sql = "select * from %1$s where jzxh = %2$s";
			sql = String.format(sql, this.tableName,jzxh);
			return super.getAllList(sql);
		}else{
			return null;
		}
	}
}
